<%@ page import="java.io.*, java.text.*, java.util.Date, java.sql.*, javax.servlet.*, javax.servlet.http.*, java.util.*" %>
<%!
public void check(String [] arr)
{
	for(int i=0;i<arr.length;i++)
	{
		while(arr[i] != null && arr[i].indexOf("\n") != -1)
		{
			arr[i] = arr[i].substring(0,arr[i].indexOf("\n")-1) + "<br/>" + arr[i].substring(arr[i].indexOf("\n")+1,arr[i].length());
		}
	}
}
%>
<%
String usernameSC = (String) session.getAttribute("session_name");
String typeSC = (String) session.getAttribute("type");
if((usernameSC == null || typeSC.equals("student") || typeSC.equals("root")))
	response.sendRedirect("error.jsp");
else
{
String driver ="com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/projects";
String username = "ia";
String password = "123";
String [] id = null;
String [] title = null;
String [] desc = null;
String [] po = null;
String [] rb = null;
String [] tn = null;
String [] dl = null;
String [] pt = null;
String [] pk = null;
String [] er = null;
String [] ac = null;
String [] proj = new String[10];
int size = 0;
try
		{
			String pid = request.getParameter("pId");
			Class.forName(driver).newInstance();
			Connection connection = DriverManager.getConnection(url, username, password);
			Statement statement = connection.createStatement();
			Statement statement1 = connection.createStatement();
		
			ResultSet rs = null;
			ResultSet rs1 = null;
	
			String query = "select count(*) from projects where id="+pid;
			String query1 = "select * from projects where id="+pid;
			rs = statement.executeQuery(query);
			if(rs.next())
			{
				size = rs.getInt(1);
			}
			
			rs1 = statement1.executeQuery(query1);
		if(rs1.next())
			{
				proj[0] = rs1.getString("Title");
				proj[1] = rs1.getString("description");
				proj[2] = rs1.getString("Project_Objectives");
				proj[3] = rs1.getString("Required_Background");
				proj[4] = rs1.getString("Tools_Needed");
				proj[5] = rs1.getString("Difficulty_Level");
				proj[6] = rs1.getString("Project_Type");
				proj[7] = rs1.getString("Project_Keywords");
				proj[8] = rs1.getString("Expected_Results");
				proj[9] = rs1.getString("Additional_Comments");
			}
			
			check(proj);

			out.println("<script type='text/javascript'>");
			out.println("var dproj = new Array();"); 
			out.println("var pid= '"+pid+"';");
			out.println("dproj[0] = '"+ proj[0]+"';");
			out.println("dproj[1] = '"+ proj[1]+"';");
			out.println("dproj[2] = '"+ proj[2]+"';");
			out.println("dproj[3] = '"+ proj[3]+"';");
			out.println("dproj[4] = '"+ proj[4]+"';");
			out.println("dproj[5] = '"+ proj[5]+"';");
			out.println("dproj[6] = '"+ proj[6]+"';");
			out.println("dproj[7] = '"+ proj[7]+"';");
			out.println("dproj[8] = '"+ proj[8]+"';");
			out.println("dproj[9] = '"+ proj[9]+"';");
			out.println("</script>");
			
	connection.close();
		

%>
<html>
<head>
<link rel="STYLESHEET" href="styles/project.css" type="text/css">
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
</head>
<body onload="onLoadDo()">
<script language="javascript" type="text/javascript">
var newProject = new Array("","","","","","","","","","");
var previousType = "Description";
var firstS = true;
var firstF = true;
var firstC = true;
function show1(type)
{
	var pre = eval("document.addProject."+previousType);
	pre.value = document.getElementById("txta").value;
	document.getElementById("txta").value = eval("document.addProject."+type+".value");
	document.getElementById(previousType).className = "free";
	document.getElementById(type).className = "choosen";
	previousType = type;
	
}

function openPreviewWindow(x,y,page)
{
	textFill();
	var maxX=screen.width; 
	var maxY= screen.height;
	var wX = window.width;
	var wY = window.height;
	var wdth;
	var heght;
	if(x == 0)
	{
		wdth = maxX - (maxX/4);
		heght = maxY -(maxY/4);
	}
	else
	{
		wdth = x;
		heght = y;
	}	
	window.open(page+".jsp",page,"height="+heght+",width="+wdth+",scrollbar=yes").moveTo((maxX/2)-(wdth/2),(maxY/2)-(heght/2));
	
}
function textFill()
{
	var pre = eval("document.addProject."+previousType);
	pre.value = document.getElementById("txta").value;
}

function addDone()
{
	textFill();
	var div = document.getElementById("errorAdd");
	if(document.addProject.Title.value=="")
	{
		div.innerHTML = "<font style='background-color: white; font-size:12pt' face='Arial' color = 'red'>Your project details are not compelete, please check the title field!</font>";
		
		return false;
	}
	else if(document.addProject.Description.value=="")
	{
		div.innerHTML = "<font style='background-color: white; font-size:12pt' face='Arial' color = 'red'>Your project details are not compelete, please check the description field!</font>";
		
		return false;
	}
	else if(document.addProject.Project_Objectives.value=="")
	{
		div.innerHTML = "<font style='background-color: white; font-size:12pt' face='Arial' color = 'red'>Your project details are not compelete, please check the project objectives field!</font>";
		
		return false;
	}
	else if(document.addProject.Required_Background.value=="")
	{
		div.innerHTML = "<font style='background-color: white; font-size:12pt' face='Arial' color = 'red'>Your project details are not compelete, please check the required background field!</font>";
		
		return false;
	}
	else if(document.addProject.Tools_Needed.value=="")
	{
		div.innerHTML = "<font style='background-color: white; font-size:12pt' face='Arial' color = 'red'>Your project details are not compelete, please check the tools needed field!</font>";
		
		return false;
	}
	else if(document.addProject.Project_Type.value=="")
	{
		div.innerHTML = "<font style='background-color: white; font-size:12pt' face='Arial' color = 'red'>Your project details are not compelete, please check the project type field!</font>";
		
		return false;
	}
	else if(document.addProject.Project_Keywords.value=="")
	{
		div.innerHTML = "<font style='background-color: white; font-size:12pt' face='Arial' color = 'red'>Your project details are not compelete, please check the project keywords field!</font>";
		
		return false;
	}
	else if(document.addProject.Expected_Results.value=="")
	{
		div.innerHTML = "<font style='background-color: white; font-size:12pt' face='Arial' color = 'red'>Your project details are not compelete, please check the expected result field!</font>";
		
		return false;
	}
		
		
	//onfocus="resetError()"
	return true;
}

function resetError()
{
	var div = document.getElementById("errorAdd");
	div.style.bgcolor = "white";
	div.innerHTML = "";
}

function onLoadDo()
{
	
	var output = "";
	var div = document.getElementById("fsize");
	output ="Font size <SELECT name='fSize' style='' scrollbar=yes><OPTGROUP Label='Font size'>";
	for(var i=8; i <= 36; i+=2)
		output += "<OPTION value="+i+">"+i+"</OPTION>";
	output+= "</OPTGROUP></SELECT>";
	div.innerHTML = ""+output;
	
	document.getElementById("title").value = dproj[0];
	document.getElementById("txta").value = dproj[1];
	document.addProject.pid.value = pid;
	document.addProject.Description.value = dproj[1];
	document.addProject.Project_Objectives.value = dproj[2];
	document.addProject.Required_Background.value = dproj[3];
	document.addProject.Tools_Needed.value = dproj[4];
	document.addProject.Difficulty_Level.value = dproj[5];
	document.addProject.Project_Type.value = dproj[6];
	document.addProject.Project_Keywords.value = dproj[7];
	document.addProject.Expected_Results.value = dproj[8];
	document.addProject.Additional_Comments.value = dproj[9];
	
	document.body.scroll = "yes";
	
}

function setB()
{
		document.getElementById('txta').value += "<b> </b>";
}
function setU()
{
		document.getElementById('txta').value += "<u> </u>";
}
function setI()
{
		document.getElementById('txta').value += "<i> </i>";
}

function setSFC()
{
	var size = document.addProject.fSize.value;
	var style = document.addProject.fStyle.value;
	var color = document.addProject.fColor.value;
	document.getElementById('txta').value += "<font style=\"font-size: "+size+"pt\" face=\""+style+"\" color=\""+color+"\"> </font>";
	
}

</script>
<center>

<form onsubmit= "return addDone()" name= "addProject" method="POST" action="updateProject.jsp">

<center><table class="Btable" border="0" height= "50%" width="99%" cellspacing="0" cellpadding="0" >
<tr><td align="left" style="color: #000000; font-size: 12pt; padding: 0px" colspan="8"><b>Title:</b>&nbsp;&nbsp;&nbsp;<input style="width:90%; padding: 3px; font-size:12pt; color: #000000" type="text" name= "Title" id= "title">
	</td>
</tr>
<tr>
	<td id="Description" class="choosen" onclick="show1('Description')">Description
	</td>
	<td id="Project_Objectives" class="free" onclick="show1('Project_Objectives')">Project<br>Objectives
	</td>
	<td id="Required_Background" class="free" onclick="show1('Required_Background')">Required<br>Background
	</td>
	<td id="Tools_Needed" class="free" onclick="show1('Tools_Needed')">Tools<br>Needed
	</td>
	<td id="Difficulty_Level" class="free" onclick="show1('Difficulty_Level')">Difficulity<br>Level
	</td>
	<td id="Project_Type" class="free" onclick="show1('Project_Type')">Project<br>Type
	</td>
	<td id="Project_Keywords" class="free" onclick="show1('Project_Keywords')">Project<br>Keywords
	</td>
	<td id="Expected_Results" class="free" onclick="show1('Expected_Results')">Expected<br>Results
	</td>
	<td id="Additional_Comments" class="free" onclick="show1('Additional_Comments')">Additional<br>Comments
	</td>
</tr>	
<tr>
	<td class="tabsbody" colspan="2" align="right">
		<center><input style="font-size: 8pt; font-weight: bold" type="button" value=" bold " onclick="setB()"/>
	
		&nbsp;&nbsp;<input style="font-size: 8pt; font-style: italic" type="button" value="italic" onclick="setI()"/>
	
		&nbsp;&nbsp;<input style="font-size: 8pt; text-decoration: underline" type="button" value="underline" onclick="setU()"/></center>
	</td>
	
	<td class="tabsbody" colspan="2"><center>
		Font color <SELECT name='fColor'><OPTGROUP Label='Font color'>
		<OPTION style="background-color: black"  value="black">black</OPTION>
		<OPTION style="background-color: blue" value="Blue">Blue</OPTION>
		<OPTION style="background-color: lightblue" value="lightblue">Light blue</OPTION>
		<OPTION style="background-color: darkblue" value="darkblue">Dark blue</OPTION>
		<OPTION style="background-color: cyan" value="cyan">Cyan</OPTION>
		<OPTION style="background-color: green" value="green">Green</OPTION>
		<OPTION style="background-color: lightgreen" value="lightgreen">Light green</OPTION>
		<OPTION style="background-color: darkgreen" value="darkgreen">Dark green</OPTION>
		<OPTION style="background-color: red" value="red">Red</OPTION>
		<OPTION style="background-color: darkred" value="darkred">Dark red</OPTION>
		<OPTION style="background-color: pink" value="pink">Pink</OPTION>
		<OPTION style="background-color: magenta" value="magenta">Magenta</OPTION>
		<OPTION style="background-color: orange" value="orange">Orange</OPTION>
		<OPTION style="background-color: yellow" value="yellow">Yellow</OPTION>
		</OPTGROUP></SELECT></center>
	</td>
		
	<td class="tabsbody" colspan="2"><center>
		Font style <SELECT name='fStyle'><OPTGROUP Label='Font style'>
		<OPTION value="Arial">Arial</OPTION>
		<OPTION value="Times New Roman">Times New Roman</OPTION>
		<OPTION value="Tahoma">Tahoma</OPTION>
		<OPTION value="Courier New">Courier New</OPTION>
		<OPTION value="Andalus">Andalus</OPTION>
		<OPTION value="Arabic Transparent">Arabic Transparent</OPTION>

		</OPTGROUP></SELECT></center>
		</td>
		<td class="tabsbody" colspan="1"><center>
		<div id="fsize">
		</div></center>
		</td>
		<td class="tabsbody" colspan="2"><center>
			<input style =" font-size: 9pt; color: black; font-weight: bold" type="button" value="   apply text style   " onclick="setSFC()"/></center>
		</td>
</tr>
<tr height="100%" style=" padding: 0px">
	<td class="tabsbody" colspan="10">
		<br><center><textarea id= "txta" onchange="textFill()" onfocus="textFill()" wrap="soft" style="font-size:12pt; width:98%; background-color: #eef3f7" ROWS="10"></textarea></center><br>
	</td>
</tr>
<tr>
	<td colspan="10">
		<input type="hidden" name="pid"/>
		<input type="hidden" name="Description"/>
		<input type="hidden" name="Project_Objectives"/>
		<input type="hidden" name="Required_Background"/>
		<input type="hidden" name="Tools_Needed"/>
		<input type="hidden" name="Difficulty_Level"/>
		<input type="hidden" name="Project_Type"/>
		<input type="hidden" name="Project_Keywords"/>
		<input type="hidden" name="Expected_Results"/>
		<input type="hidden" name="Additional_Comments"/>
		
		<center><INPUT TYPE="button" VALUE="Add Image" onclick="openPreviewWindow(250,70,'upload')"/></center>
	</td>
</tr>
<tr>
	<td colspan="9">
		<center><input type="button" value ="Preview" onclick="openPreviewWindow(0,0,'preview')"/>&nbsp;<input type="submit" value =" Update project"/></center>
	</td>
</tr>
	 </table>
</form>
</center>
<div id="errorAdd"></div>

</body>
</html>

<%
		}catch(ClassNotFoundException cnfe)
		{
			out.println("<br>Couldn't find class file" + cnfe); 
		}catch(SQLException sqle)
		{
			out.println("<br>SQL Exception: " + sqle);
		}catch(InstantiationException ie)
		{
			out.println("<br>Instant exception : " + ie);
		}catch(IllegalAccessException iae)
		{
			out.println("<br>Illegal access exception : " + iae);
		}
}%>